<?php if (! defined('BASEPATH')) exit ('No direct script access allowed');

class Category extends MY_Controller{
	
	private $pageSize =10;
	private $tableName;
	public function __construct(){
		parent::__construct();
		//连接数据库
		$this->db = $this->load->database("default",TRUE);
		$this->load->model('articles_model');
		$this->load->model('articlescategory_model');
		$this->tableName = $this->db->dbprefix('u_m_articles');
	}
	
	public function index(){
		$id = $this->input->get('id');
		$id = $this->verify_id($id);
		
		$page = $this->input->get("page");
		$page = empty($page)?1:$page;
		$page = $this->verify_id($page);
		
		$maxid = $this->getMaxCateId();
		$pagesArr = $this->getPageData($id,$page);
		if($id > $maxid){
			exit('没有该栏目!!!');
		}
		if($page>$pagesArr['pages']){
			exit('超过最大页数!!!');
		}
		$check = $this->checkCate($id);
		$result['check'] = $check;
		$result['category_name'] = $this->getCateName($check,$id);
		$result['sub_category'] = $this->getSubCate($check,$id);
		$result['location'] = $this->getLocation($id);
		$result['lzzc'] = $this->articles_model->select('*','category=3','update_time desc',5);
		
		$result['id'] = $id;
		$result['pagesArr'] = $pagesArr;
		$list = $this->articles_model->selectCate('*','category in ( select classid from u_c_articles_category where classid = '.$id.' or parentid = '.$id.' )','update_time desc',"{$pagesArr['pageMax']},{$pagesArr['pageSize']}");
		
		foreach($list as $key=>$value){
			$list[$key]['category_name'] = $this->getLocation($value['category']);
		}
		
		$result['list'] = $list;
		
//		echo "<pre>";
//		print_r($result);
//		exit;
		
		$this->load->view('category',$result);
	}
	
	//判断是否为一级栏目
	public function checkCate($id){
		$result = $this->articlescategory_model->row('classid ='.$id);
		if($result['level']==1){
			return 1;
		}else{
			return 0;
		}
	}
	
	//获取侧边导航栏目
	public function getCateName($check,$id){
		$result = $this->articlescategory_model->row('classid ='.$id);
		if($check==1){
			return $result['category_name'];
		}else if($check==0){
			$data = $this->articlescategory_model->row('classid ='.$result['parentid']);
			return $data['category_name'];
		}else{
			exit('不存在该栏目 !!!');
		}
	}
	
	//获取侧边栏目列表
	public function getSubCate($check,$id){
		if($check){
			$where = 'parentid ='.$id;
		}else{
			$data = $this->articlescategory_model->row('classid ='.$id);
			$where = 'parentid ='.$data['parentid'];
		}
		$result = $this->articlescategory_model->result($where);
		return $result;
	}
	
	//获取当前位置
	public function getLocation($id){
		$result = $this->articlescategory_model->row('classid ='.$id);
		return $result['category_name'];
	}
	
	//获取最大栏目id
	public function getMaxCateId(){
//		$sql = 'select * from u_c_articles_category where 1=1 order by classid desc';
//		$result = $this->db->query($sql)->row_array();
		$result = $this->articlescategory_model->select('classid','1=1','classid desc','0,1');
		return $result[0]['classid'];
	}
	 //获取分页数据
	public function getPageData($id,$page){
		$whereCondition = "where category in ( select classid from u_c_articles_category where classid = {$id} or parentid = {$id})";
		
		$sql = "select count(id) as pageMaxSize from {$this->tableName} {$whereCondition} ";
		$resData = $this->db->query($sql)->row_array();
		$pageSize = $this->pageSize;
		$pageMaxSize = $resData['pageMaxSize'];
		
		$pages = ceil($pageMaxSize/$pageSize);
		$pageMax = ($page-1)*$pageSize;
		$pagesArr=array(
				"pageMaxSize"=>$pageMaxSize,
				"pageSize"=>$pageSize,//每页多少条记录
				"pages"=>$pages,//共多少页
				"page"=>$page,//当前页数
				"pageMax"=>$pageMax//查询的起始索引
		);
		return $pagesArr;
	}
	//获取当前所有新闻列表
//	private function getListDatas($id,$pageSize,$pageMax){
//		$whereCondition = "where category in ( select classid from u_c_articles_category where classid = ".$id." or parentid = ".$id.")";
//		$sql = "select count(id) as pageMaxSize from {$this->tableName} {$whereCondition} ";
//		$sql.= " order by ontop desc , id desc limit {$pageMax},{$pageSize}";
//		$resData = $this->db->query($sql)->result_array();
//		return $resData;
//	}
}
?>